﻿using System;
using System.Web;
using Utilities;
using SocialShare.BusinessLogicLayer;
using SocialShare.BusinessEntities;
using SocialShare.Web.Integrations;
using SocialShare.DAL.Facades;

public partial class forum_view_post : System.Web.UI.Page
{
    StrategyProxyUser user;

    private Int32 PostId
    {
        get
        {
            try
            {
                return Convert.ToInt32(Page.RouteData.Values["postId"].ToString());
            }
            catch
            {
                return -1;
            }
        }
    }

    private String _Post_Subject
    {
        get
        {
            return HttpContext.Current.Request.Cookies["_Post_Subject"].Value;
        }
        set
        {
            HttpContext.Current.Response.Cookies["_Post_Subject"].Value = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadParentPost();
        }
    }

    public String GetUserName(String userId)
    {
        if (user == null)
        {
            user = new StrategyProxyUser();
        }

        return user.GetUserName(userId);

    }

    private void LoadParentPost()
    {
        ForumFacade bll = new ForumFacade();
        SocailShare_Forum_Messages parentPost = bll.LoadParentPost(PostId);
        if (parentPost != null)
        {
            _Post_Subject=litTitle.Text = parentPost.MessageSubject;
        }
    }
    protected void lnbReply_Click(object sender, EventArgs e)
    {
        if (PostId == -1)
        {
            return;
        }

        ForumFacade bll = new ForumFacade();
        StrategyProxyUser currentLoggedInUser = new StrategyProxyUser();
        SocailShare_Forum_Messages parentPost = bll.GetForumMessagesByPostId(PostId);

        SocailShare_Forum_Messages message = new SocailShare_Forum_Messages();
        message.CategoryId = parentPost.CategoryId;
        message.CreateDate = DateTime.Now;
        message.MessageBody = Server.HtmlEncode(txtReply.Text.Trim());
        message.Status = (Int32)ForumStatus.Published;
        message.UserId = currentLoggedInUser.GetCurrentUser().User_ID;
        message.ParentPostId = PostId;
        message.MessageSubject = _Post_Subject;
        message.IsAdminNotice = 0;

        Int32 postId = bll.CreateNewForum_Messages(message);
        if (postId > 0)
        {
            Response.Redirect("~/discussion-board/");
        }
       
    }
}